#include "UtilityPCH.h"
#include "UT_CameraEnvironment.h"
#include "UT_MathHelper.h"

using namespace Utility;

//------------------------------------------------------------
UT_CameraEnvironment::UT_CameraEnvironment(const UT_AnimatedTransform &cam2world,
	float sopen,float sclose, UT_Film *film)
	: UT_CameraBase(cam2world,sopen,sclose,film)
{

}
//------------------------------------------------------------
float UT_CameraEnvironment::GenerateRay(const UT_CameraSample &sample, UT_Ray * ray) const
{
	float time = UT_MathHelper::Lerp(sample.m_fTime, m_fShutterOpenTime, m_fShutterCloseTime);

	// Compute environment camera ray direction
	float theta = UT_MathDefine::_PI * sample.m_fImageY / m_pkFilm->yResolution;
	float phi	= 2 * UT_MathDefine::_PI * sample.m_fImageX / m_pkFilm->xResolution;
	
	float sinTheta = UT_MathHelper::Sin(theta);
	float cosTheta = UT_MathHelper::Cos(theta);
	float sinPhi   = UT_MathHelper::Sin(phi);
	float cosPhi   = UT_MathHelper::Cos(phi);

	UT_Vec3Float dir(sinTheta * cosPhi, cosTheta, sinTheta * sinPhi);
	
	*ray = UT_Ray(UT_Point3Float(0,0,0), dir, 0.f, UT_MathDefine::_INFINITY, time);
	
	m_kCameraToWorld(*ray, ray);
	
	return 1.f;
}
//------------------------------------------------------------